package com.ufcg.lvc.client.prototipacao;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.TextBox;
import com.ufcg.lvc.client.GreetingServiceCRUD;
import com.ufcg.lvc.client.GreetingServiceCRUDAsync;
import com.ufcg.lvc.client.UserData;

public class EditarAluno {

	AbsolutePanel panel;
	int instituicoesSize;
	SuggestBox suggestBox;
	UserData UD;
	
	public EditarAluno(final String nomeAntigo, final String matriculaAntiga, final String instituicaoAntiga,UserData uD) {
		UD = uD;
		final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				System.out.println("Erro na comunicação com o servidor");
			}

			@Override
			public void onSuccess(Boolean result) {
				if (result == false){
					Window.alert("Este aluno já está cadastrado.");			
				}
				else {
					panel.clear();
					MeusAlunos m = new MeusAlunos(UD);
					panel.add(m.getPanel());
				}
			}
		};

		final AsyncCallback<List<String>> callbackList = new AsyncCallback<List<String>>() {
			@Override
			public void onFailure(Throwable caught) {
				System.out.println("Erro na comunicação com o servidor");
			}

			@SuppressWarnings("deprecation")
			@Override
			public void onSuccess(List<String> result) {
				MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
		        for (int i = 0; i < result.size(); i++) {
					oracle.add(result.get(i).toString());
				}
		        suggestBox = new SuggestBox(oracle);
		        suggestBox.setAnimationEnabled(true);
		        suggestBox.setVisible(true);
		        suggestBox.setSize("319px", "17px");
		        suggestBox.setText(instituicaoAntiga);
		        panel.add(suggestBox, 143, 139);
			}
		};
		
		getServicoCRUD().getNomeIntituicoes(UD.getId_usuario(), callbackList);
		
		panel = new AbsolutePanel();
		panel.setSize("900px", "707px");
		
		Label tituloLB = new Label("Editar aluno");
		tituloLB.setStyleName("gwt-TituloMedio");
		panel.add(tituloLB, 0, 10);
	
		final TextBox nomeTB = new TextBox();
		panel.add(nomeTB, 143, 69);
		nomeTB.setText(nomeAntigo);
		nomeTB.setSize("319px", "17px");
				
		Label lblNome = new Label("Nome:");
		panel.add(lblNome, 60, 69);
		
		Label lblMatrcula = new Label("Matrícula:");
		panel.add(lblMatrcula, 60, 104);
		lblMatrcula.setSize("47px", "17px");
		
		final TextBox matriculaTB = new TextBox();
		matriculaTB.setText(matriculaAntiga);
		panel.add(matriculaTB, 143, 104);
		matriculaTB.setSize("319px", "17px");
		
		Label lblInstituicao = new Label("Instituição:");
		panel.add(lblInstituicao, 60, 139);
		lblInstituicao.setSize("47px", "17px");

		com.google.gwt.user.client.ui.Button btnEditarAluno = new com.google.gwt.user.client.ui.Button("Salvar");
		panel.add(btnEditarAluno, 183, 	189);
		btnEditarAluno.setSize("121px", "29px");
		
		Button btnVoltar = new com.google.gwt.user.client.ui.Button("Voltar");
		panel.add(btnVoltar, 307, 189);
		btnVoltar.setSize("121px", "29px");
		
		btnEditarAluno.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if(!nomeTB.getText().trim().equals("") && !matriculaTB.getText().trim().equals("") && !suggestBox.getText().trim().equals("")){
					getServicoCRUD().updateAluno(nomeAntigo, matriculaAntiga, instituicaoAntiga, nomeTB.getText(), matriculaTB.getText(), suggestBox.getText(), UD.getId_usuario(), callback);
					panel.clear();
					MeusAlunos m = new MeusAlunos(UD);
					panel.add(m.getPanel());
				} else {
					Window.alert("Todos os campos devem ser preenchidos.");
				}
			}
		});
		
		btnVoltar.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				panel.clear();
				MeusAlunos m = new MeusAlunos(UD);
				panel.add(m.getPanel());
			}
		});
	}
	
	public static GreetingServiceCRUDAsync getServicoCRUD() {
		return GWT.create(GreetingServiceCRUD.class);
	}

	public AbsolutePanel getPanel() {
		return panel;
	}
}
